<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Controllers\Controller;

use DB;

use Markdown;
use League\Flysystem\Directory;
use Auth;

use Captcha;
use Symfony\Component\VarDumper\Tests\Caster\reflectionParameterFixture;
use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener;
class NewController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
    	dd('test');
        return view('home.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    
    //删除单条信息
    public function del($id)
    {
    	$res = Art::find($id)->delete();
    	$res = Art::orderBy('id','desc')->paginate(5);
    	return view('new.show',['res'=>$res]);
    
    }
    
    
    /**
     * 登出
     */
    public function getLogout()
    {
    	Auth::Logout();
    	return redirect('/auth/login');
    }
    
    /**
     * 登入
     */
    public function postLogin(Request $request)
    {
    	//判断验证码
    	$code = Captcha::check($request->verify);
    	if($code){
    		//判断信息是否正确
    		//邮箱正则
    		$em= "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
    		//手机号正则
    		$phone = "^[1][3578]\\d{9}$";
    		//获取输入值
    		$req = $request->email;
    		//判断
    		if(preg_match($em,$req)){
    			//判断邮箱登录
    			$res = Auth::attempt(['email'=>$request->req,'password'=>$request->password]);
    		}else{
    			if(preg_macth($phone,$req)){
    				//手机号登录
    				$res = Auth::attempt(['mobile'=>$request->req,'password'=>$request->password]);
    			}else{
    				//用户名登录
    				$res = Auth::attempt(['name'=>$request->req,'password'=>$request->password]);
    			}
    		}    		
//     		$res = Auth::attempt(['email'=>$request->email,'password'=>$request->password]);
    		if($res){
    			return redirect('/index');
    		}else{
    			return back();
    		}
    	}else{
    		return back();
    	}
    }
    
    /**
     * 验证码
     */
    public function getCode()
    {
    	return Captcha::img();
    }
}
